#include <stdlib.h>
#include <string.h>

#include <directfb.h>

#include "dtk_absolute_layout.h"
#include "dtk_debug.h"

const DString DTK_LAYOUT_TYPE_ABSOlUTE = "DTKLayoutAbsolute";

static DInt zIndex_compare(DLink* link1, DLink* link2)
{
    DTKLayout *layout1, *layout2;

    DTK_ASSERT(link1 != NULL);
    DTK_ASSERT(link2 != NULL);

    layout1 = (DTKLayout*) link1;
    layout2 = (DTKLayout*) link2;

    if (layout1->zIndex > layout2->zIndex)
    {
        return 1;
    }

    if (layout1->zIndex == layout2->zIndex)
    {
        return 0;
    }

    return -1;
}

static void absolute_release (DTKLayout* layout)
{
    if (layout == NULL)
    {
        return;
    }
    dtk_layout_remove_sub_all(layout);
    return;
}

static void absolute_add (DTKLayout* self, DTKLayout* layout)
{
    dtk_list_insert_by_order (&self->list,
        &layout->link,
        zIndex_compare);
    layout->parent = self;

    return;
}

static void absolute_remove (DTKLayout* self, DTKLayout* layout)
{
    dtk_list_remove (&self->list, &layout->link);
    return;
}

void absolute_update (DTKLayout* layout)
{
    return;
}



DTKLayout* dtk_absolute_layout_new(DTKActor* actor)
{
    DTKLayout* layout;
    DTKLayoutAbsolute* absolute;

    layout = dtk_layout_new (sizeof(DTKLayoutAbsolute),
        DTK_LAYOUT_TYPE_ABSOlUTE, actor);

    layout->update = absolute_update;

    layout->add = absolute_add;
    layout->remove = absolute_remove;

    layout->release = absolute_release;

    return layout;    
}
